<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");

require_once '../../../lib/php/vars.php';
require_once '../../../lib/php/dbCon.php';
require_once 'deleteFile.php';

//"prefix" gibt an was gelöscht werden soll
if(isset($_GET['prefix'])) {
	if (isset($_GET['id']) && isset($_GET['path'])) {
		$path_image = $documentRoot.$sitePath.$_GET['path'];
		$json = array('path' => $path_image);
		
		$path_preview = substr_replace($path_image, '_preview', strrpos($path_image, '.'), 0);
		$json['path_preview'] = $path_preview;
		
		$allowedExtensions = array("jpg","png","gif","jpeg","pdf","swf","flv","avi","wmv","mov");
			if ($path_image > '') {
				if (!in_array(end(explode(".", strtolower($path_image))), $allowedExtensions)) {
					$json['error'] = 'File not found, or filename/path is incorrect: '.$path_image;
					$json['result'] = 0;
				} else {
					if (file_exists($path_image)) {
						if(rmdirr($path_image)) {
							$json['result'] = 1;
							
							$query = 'UPDATE`'.$db.'`.`ak_buttons`'
							.' SET btn_img="", btn_img_enable='. 0 .' '
							.'WHERE btn_id = '.$_GET['id'];
							
							if($result = mysql_query($query)) {
								$json['result'] = 1;
								
								if (file_exists($path_preview)) {
									if(rmdirr($path_preview)) {
										$json['result'] = 1;
									} else {
										$json['error'] = 'delete preview failed';
										$json['result'] = 0;
									}
								} else {
									$json['error'] = 'preview not found';
									$json['result'] = 0;
								}
								
							} else {
								$json['error'] = 'delete image from database failed: '.mysql_error();
								$json['result'] = 0;
							}
						} else {
							$json['error'] = 'delete image failed';
							$json['result'] = 0;
						}
					} else {
						$json['error'] = 'image not found';
						$json['result'] = 0;
					}
				}
			}
	} else {
		$json = array('result' => 0,
						'error' => 'Parameter missing! ' 
						.'No "id" and/or no "path" submitted');
	}
} else {
	$json = array('result' => 0,
					'error' => 'Parameter missing! No type submitted!');
}
// Datenbankverbindung schließen
mysql_close($conn);
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>